<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <my-parent-comp></my-parent-comp>
    </div>
    <template id="comp1">
        <div><h1>我的父组件</h1>
            <input v-model="mytitle" type="text" /><br>
            <my-child-comp v-bind:title="mytitle" @pchange="doParentChange"></my-child-comp></div>
    </template>
    <template id="comp2">
        <h3>
            <input type="text" v-model="title" @keyup="doChildChange"><br>
            {{title}}
            子组件</h3>
    </template>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
    var vm = new Vue({
        el:'#app',
        components:{
            "my-parent-comp":{
                // 声明父组件
                data:function() {
                    return {
                        mytitle:'你好'
                    }
                },
                template:"#comp1",
                methods: {
                  doParentChange:function(text){
                      console.log(text)
                      this.mytitle = text
                  }  
                },
                components:{
                    // 声明子组件
                    "my-child-comp":{
                        props: ["title"],
                        template:"#comp2",
                        methods: {
                            doChildChange:function(){
                                // 触发父组件事件
                                this.$emit("pchange", this.title)
                            }
                        }
                    }                    
                }
            }
        }
    })
    </script>
</body>
</html>